package com.shaoshaohuo.app.utils.sharesdk;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

/**
 * Created by Administrator on 2017/6/3.
 */

public class NumberView extends View {
    private Paint mPaint;
    private Context mContext;
    private Canvas canvas;
    private WindowManager wm;
   private String num;
    private TextPaint textPaint;

    public NumberView(Context context) {
        this(context,null);

    }

    public NumberView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);

    }

    public NumberView(Context context, @Nullable AttributeSet attrs,int a) {
        super(context, attrs,a);
        mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(Color.parseColor("#fe555a"));
        //字體的paint
        textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(20);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //
        int ws = getsize(widthMeasureSpec, 0);
        int hs = getsize(widthMeasureSpec, 1);
        setMeasuredDimension(ws,hs);

    }

    private int getsize(int widthMeasureSpec,int hw) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        if(mode==MeasureSpec.AT_MOST){
            if(hw==0){
                return    ((ViewGroup)getParent()).getMeasuredWidth()/4;
            }else {
                return    ((ViewGroup)getParent()).getMeasuredHeight()/4;
            }


        }

        return -1;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int radios=getWidth()>getHeight()?getHeight():getWidth();
        canvas.drawCircle(getWidth()/2,getHeight()/2,radios/2,mPaint);
        canvas.drawText(num,getWidth()/2-textPaint.measureText(num)*0.5f,getHeight()/2+textPaint.getFontMetrics().bottom*1.5f,textPaint);
    }
    public void setNum(String num){
        this.num=num;
        invalidate();
    }
}
